//
//  ViewController.m
//  YM分享
//
//  Created by 邢帅 on 2019/3/12.
//  Copyright © 2019 邢帅. All rights reserved.
//

#import "ViewController.h"

#import <UMShare/UMShare.h>
#import <UShareUI/UShareUI.h>



#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width            // 屏幕宽
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height          // 屏幕高

@interface ViewController ()

@property (nonatomic,strong) UILabel *labl;

@end

@implementation ViewController

//-(void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshUserNotificationSettings) name:UIApplicationWillEnterForegroundNotification object:nil];
//
//}
//-(void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
//}

-(void)refreshUserNotificationSettings
{
//    if ([[[UIApplication sharedApplication] currentUserNotificationSettings] types] == 0)
//    {
//        NSLog(@"已关闭");
//    }
//    else {
//        NSLog(@"已开启");
//    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self presentViewController:[NSClassFromString(@"FViewController") new] animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self refreshUserNotificationSettings];
    
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:button];
    button.frame = CGRectMake(20, 50, SCREEN_WIDTH - 40, 50);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:17];
    [button setTitle:@"友盟分享" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor redColor]];
    [button addTarget:self action:@selector(sharePanUI) forControlEvents:UIControlEventTouchUpInside];
    
    
    NSArray *arr = @[@"微信登录",@"QQ登录",@"微博登录"];
    
    for (int i = 0; i < arr.count; i ++)
    {
        UIButton *dlbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [self.view addSubview:dlbtn];
        dlbtn.frame = CGRectMake(20, (100*(i+1)+50), SCREEN_WIDTH - 40, 50);
        [dlbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        dlbtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [dlbtn setTitle:arr[i] forState:UIControlStateNormal];
        [dlbtn setBackgroundColor:[UIColor blackColor]];
        dlbtn.tag = i + 1;
        [dlbtn addTarget:self action:@selector(denglu:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    // 测试第三方登录是否成功，成功则显示获取的昵称验证一下
    UILabel *labl = [[UILabel alloc]init];
    [self.view addSubview:labl];
    labl.frame = CGRectMake(20, 450, SCREEN_WIDTH - 40, 50);
    labl.text = @"昵称是 ？？？";
    labl.textAlignment = NSTextAlignmentCenter;
    labl.font = [UIFont systemFontOfSize:17];
    labl.textColor = [UIColor blackColor];
    labl.backgroundColor = [UIColor whiteColor];
    self.labl = labl;
    
}

#pragma mark -- 分享模板
- (void)sharePanUI
{
    __weak typeof(self)weakSelf = self;
    //显示分享面板
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        // 根据获取的platformType确定所选平台进行下一步操作
        
        [weakSelf shareWebPageToPlatformType:platformType];
    }];
}


#pragma mark -- 分享实现
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    NSString* thumbURL = @"https://mobile.umeng.com/images/pic/home/social/img-1.png";
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"韩信打野贼厉害" descr:@"大家都躺好，我要带飞了" thumImage:thumbURL];
    //设置网页地址
    shareObject.webpageUrl = @"http://mobile.umeng.com/social";
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
            
            NSLog(@"code == %ld",error.code);
            
        }
        else
        {
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);

            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}

#pragma mark -- 友盟实现第三方登录
-(void)denglu:(UIButton *)btn
{
    switch (btn.tag)
    {
        case 1:
            [self getUserInfoForPlatform:UMSocialPlatformType_WechatSession];    // 微信登录
            break;
        case 2:
            [self getUserInfoForPlatform:UMSocialPlatformType_QQ];               // QQ登录
            break;
        case 3:
            [self getUserInfoForPlatform:UMSocialPlatformType_Sina];             // 微博登录
            break;
        default:
            break;
    }
}

#pragma mark -- 登录授权并获取z需要的资料
- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    __weak typeof(self) weakSelf = self;
    
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:nil completion:^(id result, NSError *error) {

        if(error)
        {
            
        }
        else
        {
            UMSocialUserInfoResponse *resp = result;
            
            // 第三方登录数据(为空表示平台未提供)
            // 授权数据
            NSLog(@" uid: %@", resp.uid);
            NSLog(@" openid: %@", resp.openid);
            NSLog(@" accessToken: %@", resp.accessToken);
            NSLog(@" refreshToken: %@", resp.refreshToken);
            NSLog(@" expiration: %@", resp.expiration);
            
            // 用户数据
            NSLog(@" name: %@", resp.name);
            NSLog(@" iconurl: %@", resp.iconurl);
            NSLog(@" gender: %@", resp.unionGender);
            
            weakSelf.labl.text = @"";
            weakSelf.labl.text = resp.name;
            
            // 第三方平台SDK原始数据
            NSLog(@" originalResponse: %@", resp.originalResponse);
        }
        
    }];
}

// 点击空白跳转iphone自带safari浏览器
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
//}

@end
